package thn.glf.org.lib;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteDB extends SQLiteOpenHelper {
	public static String DB_PATH = "/data/data/thn.glf.org.personalfinancial/databases/";
	public static String DB_NAME = "PersonalFinancial.sqlite";
	public ArrayList<String> List_Sql_table;
	public ArrayList<String> ListTableName;
	private final Context myContext;
	private SQLiteDatabase mDB;

	public SQLiteDB(Context context) {
		super(context, DB_NAME, null, 1);
		this.myContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	// check exits database
	private boolean ExistDataBase() {

		SQLiteDatabase checkDB = null;
		try {
			String myPath =SQLiteDB.DB_PATH + SQLiteDB.DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null,
					SQLiteDatabase.OPEN_READONLY);
		} catch (SQLiteException e) {
			// database does't exist yet.
		}
		if (checkDB != null) {
			checkDB.close();
		}
		return checkDB != null ? true : false;
	}

	public ArrayList<String> getListTableName() {
		return ListTableName;
	}

	public void setListTableName(ArrayList<String> listTableName) {
		ListTableName = listTableName;
	}

	public ArrayList<String> getList_Sql_table() {
		return List_Sql_table;
	}

	public void setList_Sql_table(ArrayList<String> list_Sql_table) {
		List_Sql_table = list_Sql_table;
	}

	public void open() {
		try {
			createDataBase();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String myPath = DB_PATH + DB_NAME;
		mDB = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READWRITE);
	}

	public synchronized void close() {

		if (mDB != null)
			mDB.close();
		super.close();
	}

	public int delete(String tablename, String whereClause, String[] whereArgs) {
		return mDB.delete(tablename, whereClause, whereArgs);
	}

	public long Insert(String tablename, ContentValues params) {
		return mDB.insert(tablename, null, params);
	}

	public int Update(String tablename, ContentValues params,
			String whereClause, String[] whereArgs) {
		return mDB.update(tablename, params, whereClause, whereArgs);
	}

	public Cursor query(boolean distinct, String table, String[] columns,
			String selection, String[] selectionArgs, String groupBy,
			String having, String orderBy, String limit) {
		Cursor mCursor = mDB.query(distinct, table, columns, selection,
				selectionArgs, groupBy, having, orderBy, limit);
		return mCursor;

	}

	public Cursor query(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy) {
		Cursor mCursor = mDB.query(table, columns, selection, selectionArgs,
				groupBy, having, orderBy);

		return mCursor;
	}

	public Cursor query(String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy, String limit) {
		// mDB=this.getWritableDatabase();
		Cursor mCursor = mDB.query(table, columns, selection, selectionArgs,
				groupBy, having, orderBy, limit);

		return mCursor;
	}

	public Cursor queryWithFactory(SQLiteDatabase.CursorFactory cursorFactory,
			boolean distinct, String table, String[] columns, String selection,
			String[] selectionArgs, String groupBy, String having,
			String orderBy, String limit) {
		Cursor mCursor = mDB.queryWithFactory(cursorFactory, distinct, table,
				columns, selection, selectionArgs, groupBy, having, orderBy,
				limit);
		return mCursor;

	}
//
	public Cursor rawQuery(String sql, String[] selectionArgs) {
		Cursor mCursor = mDB.rawQuery(sql, selectionArgs);

		return mCursor;
	}
	//
	public void createDataBase() throws IOException {

		boolean dbExist = ExistDataBase();

		if (dbExist) {
			// do nothing - database already exist
		} else {

			// By calling this method and empty database will be created into
			// the default system path
			// of your application so we are gonna be able to overwrite that
			// database with our database.
			this.getReadableDatabase();

			try {

				copyDataBase();

			} catch (IOException e) {

				throw new Error("Error copying database");

			}
		}

	}

	private void copyDataBase() throws IOException {

		// Open your local db as the input stream
		InputStream myInput = myContext.getAssets().open(DB_NAME);

		// Path to the just created empty db
		String outFileName = DB_PATH + DB_NAME;
		Log.e("FILE PATH", outFileName);
		// Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(outFileName);

		// transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}
}
